
package com.ido.babyshow.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ido.babyshow.common.GlobalConstant;
import com.ido.babyshow.dao.IMotherDao;
import com.ido.babyshow.dao.IPhotoDao;
import com.ido.babyshow.dao.IWeiboDao;
import com.ido.babyshow.model.Mother;
import com.ido.babyshow.model.Photo;
import com.ido.babyshow.model.Weibo;
import com.ido.babyshow.service.IMotherService;
import com.ido.babyshow.util.Utils;

@Service
public class MotherService implements IMotherService
{
    @Resource
    private IMotherDao motherDao;

    @Resource
    private IWeiboDao weiboDao;
    
    @Resource
    private IPhotoDao photoDao;    

    public void setMotherDao(IMotherDao motherDao)
    {
        this.motherDao = motherDao;
    }

    @Override
    public boolean validateEmail(String email)
    {
        return motherDao.validateEmail(email) > 0 ? true : false;
    }

    @Override
    public boolean registerMother(Mother mother)
    {
        return motherDao.insert(mother) > 0 ? true : false;
    }

    @Override
    public Mother selectByEmail(String email)
    {
        return motherDao.selectOne(email);
    }

    @Override
    public Mother login(String email, String password)
    {
        Mother loginMother = new Mother();
        loginMother.setEmail(email);
        loginMother.setPassword(password);
        return motherDao.login(loginMother);
    }

    @Override
    public void addPro(Mother mother)
    {
        mother.setLikeMen(motherDao.selectLikeMens(mother.getEmail()));
        mother.setFans(motherDao.selectFans(mother.getEmail()));
        Map paraMap = new HashMap();
        paraMap.put("email", mother.getEmail());
        paraMap.put("maxId", -1);
        mother.setWeibo(weiboDao.<Weibo>selectList(paraMap));
        mother.setPhotos(photoDao.selectByMother(mother.getEmail()));
        // mother.setBabys(babys);
    }

    @Override
    public boolean publishMessage(String email, String content)
    {
        Weibo weibo=new Weibo();
        weibo.setContent(content);
        weibo.setSender(email);
        return weiboDao.insert(weibo)>0?true:false;
    }
    
    public boolean shareImage(Photo photo,File imageFile,String fileType,Mother mother) throws IOException{  
        String fileName=Utils.createFileName(fileType);
        photo.setImageURL(GlobalConstant.URL_PATH_SEPA+GlobalConstant.UPLOAD_PATH+GlobalConstant.URL_PATH_SEPA+mother.getId()+GlobalConstant.URL_PATH_SEPA+fileName);
        Utils.uploadFile(imageFile, mother.getId(),fileName);
        photo.setOwner(mother);
        return  photoDao.insert(photo)>0?true:false;
      }


}
